<?php
class BDO
{
    private $link;
    public function __construct()
    {
        $this->link=$dbh = new PDO('mysql:host=127.0.0.1;dbname=1705php', 'root', 'root');
        $this->link->exec("set names utf8");
        $this->link->setAttribute(PDO::ATTR_ERRMODE,1);

    }
    public function getOne($table,$where)
    {
        if(empty($table) || empty($where))
        {
            return "不能为空";
        }
        $sql="select * from `$table` where $where";
        $res=$this->link->query($sql);
        return $res->fetch(2);
    }
    public function add($table,$array)
    {
        if(empty($table) || count($array))
        {
            return "不能为空";
        }
        $field="";
        $val="";
        foreach($array as $k=>$v)
        {
            $field.="`$k`,";
            $val.="'$v',";
        }
        $field=substr($field,0,-1);
        $val=substr($val,0,-1);
        $sql="insert into `$table` ($field) VALUE ($val)";
        return $this->link->exec($sql);
    }
    public function del($table,$where)
    {
        if(empty($table) || empty($where))
        {
            return "不能为空";
        }
        $str="";
        foreach($where as $k=>$v)
        {
            $str.="`$k`='$v'";
        }
        $sql="delete from `$table` WHERE $str";
        return $this->link->exec($sql);
    }
    public function updata($table,$where,$arr)
    {
        if(empty($table) || empty($where))
        {
            return "不能为空";
        }
        $str="";
        foreach($where as $k=>$v)
        {
            $str.="`$k`='$v'";
        }
        $sql="update `$table` set $str where $arr";
        return $this->link->exec($sql);
    }



}
//查询
//$db=new BDO();
//$res=$db->getOne("games");
//var_dump($res)

//添加
//$db=new BDO();
//$array=['g_name'=>'啊啊',['g_desc'=>'哦哦'],['g_autho'=>'问问']];
//$res=$db->add("games",$array);

//删除
//$db=new BDO();
//$res=$db->del("games",['g_id'=>1]);
//var_dump($res);

//修改
//$db=new BDO();
//$res=$db->updata("games",['g_name'=>'加油'],'g_id=3');
//var_dump($res);

?>